package com.jysz.certificate.persistence.model.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.jysz.webcore.ssh.entity.AbstractBaseEntity;


/**
 * 角色
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Entity
@Table(name="sys_role")
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
public class Role extends AbstractBaseEntity<Long> {

	private static final long serialVersionUID = 1L;

	/** 角色名称*/
	private String roleName;
	
	/** 描述*/
	private String memo;
	
	/** 是否是初始化的.驻店员、监管科、区域经理、财务、绩效*/
	private Boolean initialize;
	
	/** 人员*/
	private Set<User> users;
	
	/** 权限*/
	private List<RoleOperRef> roleOperRefList = new ArrayList<RoleOperRef>();
	
	@Column(name="role_name",columnDefinition="varchar(255)")
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	@Column(name="memo",columnDefinition="varchar(255)")
	public String getMemo() {
		return memo;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
	
	@Column(name = "_initialize", nullable = false)
	public Boolean getInitialize() {
		return initialize;
	}

	public void setInitialize(Boolean initialize) {
		this.initialize = initialize;
	}
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "rs_sys_user__sys_role_", joinColumns = @JoinColumn(name = "_role_id"), inverseJoinColumns = @JoinColumn(name = "_user_id"))
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	@SuppressWarnings("deprecation")
	@OneToMany(mappedBy="role", fetch=FetchType.LAZY)
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
		org.hibernate.annotations.CascadeType.DELETE,
        org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	public List<RoleOperRef> getRoleOperRefList() {
		return roleOperRefList;
	}
	public void setRoleOperRefList(List<RoleOperRef> roleOperRefList) {
		this.roleOperRefList = roleOperRefList;
	}
	
	@Transient
	public boolean containUser(Long adminId) {
		for(User e : getUsers()) {
			if(e.getId().equals(adminId)) {
				return true;
			}
		}
		return false;
	}
}
